<?xml version="1.0"?>
<!--
chartsvg.xsl
$Revision: 4 $
Main ChartSVG xslt file. This is what you must call with your xslt processor

Created by: Virgil Dupras
Created on: 2004/09/11

Last modified by:$Author: hardcoded.software $
Last modified on:$Date: 2006-08-26 14:05:46 +0000 (Sat, 26 Aug 2006) $

Copyright (C) 2004-2006 Virgil Dupras (http://www.hardcoded.net)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/2000/svg" exclude-result-prefixes="#default">
	
<xsl:import href="lib_axis.xsl"/>
<xsl:import href="lib_bar.xsl"/>
<xsl:import href="lib_line.xsl"/>
<xsl:import href="lib_pie.xsl"/>

<xsl:output 
	method="xml"
	doctype-public="-//W3C//DTD SVG 1.0//EN"
	doctype-system="http://www.w3.org/TR/SVG/DTD/svg10.dtd"
	indent="yes"
	cdata-section-elements="style"/>

<xsl:template match="xaxis/grid">
	<xsl:call-template name="draw_x_grid">
		<xsl:with-param name="pos_method">
			<xsl:if test="/hsgraph/charttype = 'line'">1</xsl:if>
			<xsl:if test="/hsgraph/charttype = 'bar'">0</xsl:if>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="yaxis/grid">
	<xsl:call-template name="draw_y_grid"/>	
</xsl:template>
	
<xsl:template match="series/serie">
	<xsl:if test="/hsgraph/charttype = 'line'">
		<xsl:call-template name="draw_line"/>
	</xsl:if>
	<xsl:if test="/hsgraph/charttype = 'bar'">
		<xsl:call-template name="draw_bar"/>
	</xsl:if>
</xsl:template>
	
<xsl:template match="series">
	<xsl:if test="(/hsgraph/charttype = 'line') or (/hsgraph/charttype = 'bar')">
		<xsl:apply-templates select="serie"/>
	</xsl:if>
	<xsl:if test="(/hsgraph/charttype = 'pie')">
		<xsl:call-template name="draw_pie"/>
	</xsl:if>
</xsl:template>

<xsl:template match="hsgraph">
	<svg preserveAspectRatio="xMidYMid meet">
		<xsl:attribute name="viewBox">
			<xsl:if test="showlegend and not(showlegend/@chartfitpos)">0 0 800 400</xsl:if>
			<xsl:if test="not(showlegend) or showlegend/@chartfitpos">0 0 600 400</xsl:if>
		</xsl:attribute>
		<xsl:comment>
 Created by ChartSVG - http://www.hardcoded.net/chartsvg/ 
		</xsl:comment>
		<defs>
			<style type="text/css">
				.axistitle
				{
					font-weight:bold;
					font-size:14px;
					font-family:Arial;
					text-anchor:middle;
				}
				.xgrid, .ygrid, .legendtext
				{
					font-weight:normal;
					font-size:11px;
					font-family:Arial;
				}
				.xgrid {text-anchor:middle;}
				.ygrid {text-anchor:end;}
				.gridline
				{
					stroke:black;
					stroke-width:1;
				}
				.values
				{
					fill:black;
					stroke:none;
					text-anchor:middle;
					font-size:12px;
					font-weight:bold;
				}
	   		</style>
		</defs>
		<xsl:if test="bgcolor">
			<rect x="0" y="0" height="400">
				<xsl:attribute name="width">
					<xsl:if test="showlegend and not(showlegend/@chartfitpos)">800</xsl:if>
					<xsl:if test="not(showlegend) or showlegend/@chartfitpos">600</xsl:if>
				</xsl:attribute>
				<xsl:attribute name="stroke">none</xsl:attribute>
				<xsl:attribute name="fill">
					<xsl:value-of select="bgcolor"/>
				</xsl:attribute>
			</rect>
		</xsl:if>
		<text class="axistitle" x="300" y="20"><xsl:value-of select="title"/></text>
		<xsl:if test="(/hsgraph/charttype = 'line') or (/hsgraph/charttype = 'bar')">
			<xsl:apply-templates select="xaxis"/>	
			<xsl:apply-templates select="yaxis"/>
			<svg id="graphzone" preserveAspectRatio="none" x="50" y="50" width="500" height="300" viewBox="0 0 500 300">
				<xsl:if test="yaxis/margin">
					<xsl:attribute name="x"><xsl:value-of select="50 + yaxis/margin"/></xsl:attribute>
					<xsl:attribute name="width"><xsl:value-of select="500 - yaxis/margin"/></xsl:attribute>
				</xsl:if>
				<xsl:if test="showlegend and showlegend/@chartfitpos">
					<xsl:call-template name="draw_legend">
						<xsl:with-param name="x_offset">
							<xsl:value-of select="(500 div xaxis/max) * showlegend/@chartfitpos"/>
						</xsl:with-param>
						<xsl:with-param name="y_offset">-15</xsl:with-param>
					</xsl:call-template>
				</xsl:if>
				<xsl:apply-templates select="series"/>
			</svg>
		</xsl:if>
		<xsl:if test="(/hsgraph/charttype = 'pie')">
			<svg id="graphzone" preserveAspectRatio="xMidYMid meet" x="0" y="50" width="600" height="340" viewBox="0 0 500 300">
				<xsl:if test="showlegend">
					<xsl:call-template name="draw_legend"/>
				</xsl:if>
				<xsl:apply-templates select="series"/>
			</svg>
		</xsl:if>
		<rect style="fill:none;stroke-width:1;stroke:black;" x="0" y="0" width="600" height="400"/>
		<xsl:if test="showlegend and not(showlegend/@chartfitpos)">
			<svg id="legendzone" x="600" y="0" width="200" height="400" viewBox="0 0 200 400">
				<rect style="fill:none;stroke-width:1;stroke:black;" x="0" y="0" width="200" height="{(count(series/serie) + 2) * 15}"/>	
				<xsl:call-template name="draw_legend"/>
			</svg>
		</xsl:if>
	</svg>
</xsl:template>

<!-- NAMED TEMPLATES -->

<xsl:template name="draw_legend">
	<xsl:param name="x_offset" select="0"/>
	<xsl:param name="y_offset" select="0"/>
	<xsl:variable name="total_y" select="sum(*/serie/point/@y)"/>
	<xsl:for-each select="series/serie">
		<xsl:variable name="ypos" select="(position() * 15) + $y_offset"/>
		<rect style="fill:{color};stroke-width:1;stroke:black;" x="{10 + $x_offset}" y="{$ypos}" width="10" height="10"/>
		<text class="legendtext" x="{25 + $x_offset}" y="{$ypos + 10}">
			<xsl:value-of select="name"/>
			<xsl:if test="/hsgraph/showlegend/@showpercent">
				(<xsl:value-of select="round((sum(point/@y) div $total_y) * 100)"/>%)
			</xsl:if>
		</text>
	</xsl:for-each>
</xsl:template>

</xsl:stylesheet>